package com.ld.businessCard.service.impl;

import com.ld.businessCard.dao.UserDAO;
import com.ld.businessCard.entity.User;
import com.ld.businessCard.service.CardService;
import com.ld.businessCard.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.List;

public class UserServiceImpl implements UserService {
	private UserDAO userDAO = new UserDAO();
	private CardService cardService = new CardServiceImpl();
	public int changeUserById(User user) {
		return 0;
	}


	public User login(String account, String password) {
		String s = DigestUtils.md5Hex(password);
		return userDAO.findOne("from User U where U.account = ? and U.password = ?",new String[]{account,s});
	}
	public List<User> getAllUsers(){
		return userDAO.find("from User U where U.status = 0",new String[]{});
	}


	public int deleteUserById(Integer id) {
		int i = userDAO.delete(User.class, id);
		int j = cardService.deleteCardByUserId(id);
		if(i + j == 2){
			return 1;
		}else return 0;

	}



	public int register(User user) {
		//防止改变注册的用户的密码信息
		User user1 = new User();
		user1.setPassword(DigestUtils.md5Hex(user.getPassword()));
		user1.setAccount(user.getAccount());
		user1.setStatus(user.getStatus());
		user1.setUsername(user.getUsername());
		return userDAO.insert(user1);
	}


	public User getUserByAccount(String account) {
		return userDAO.findOne("from User U where U.account = ?",new String[]{account});
	}
	public User getUserById(Integer id) {
		return userDAO.findById(User.class,id);
	}
}
